﻿#include "VAOneXMLAdaptorNCT.h"
#include "FITK_Kernel/FITKAppFramework/FITKAppFramework.h"
#include "FITK_Kernel/FITKAppFramework/FITKGlobalData.h"
#include "rapidxml.hpp"
#include "FITKAcousticsPhysics/FITKAcousticsPhysics.h"
#include "FITKAcousticsPhysics/FITKAcousticsNCTLayUp.h"
#include "FITKAcousticsPhysics/FITKAcousticsAbsNCT.h"
#include "FITKAcousticsPhysics/FITKAcousticsAbsMaterial.h"
#include "FITKAcousticsPhysics/FITKAcousticsAbsPhysicsProp.h"
#include "FITKAcousticsPhysics/FITKAcousticsAbsNCT.h"
#include "FITK_Interface/FITKInterfacePhysics/FITKAbstractMaterial.h"
#include "FITKAcousticsPhysics/FITKAcousticsNCTLayUp.h"
#include "FITKAcousticsPhysics/FITKAcousticsNCTSeptum.h"
#include "FITKAcousticsPhysics/FITKAcousticsNCTResistive.h"
#include "FITKAcousticsPhysics/FITKAcousticsNCTPerforated.h"
#include "FITKAcousticsPhysics/FITKAcousticsNCTUserDefined.h"
#include "FITKAcousticsPhysics/FITKAcousticsSpectraManager.h"
#include "FITKAcousticsPhysics/FITKAcousticsAbstractSpectra.h"
#include "FITKAcousticsPhysics/FITKAcousticsNCTMultipleNoise.h"
#include "VAOneXMLAdaptorAcousticsNCTLayUp.h"
#include "VAOneXMLAdaptorAcousticsNCTUserDefined.h"
#include "VAOneXMLAdaptorAcousticsNCTMultipleNoiseControl.h"
#include <QDebug>

namespace IO
{

    bool VAOneXMLAdaptorNCT::adaptR()
    {
        // 指针判断是否为空
        if(!_node)
        {
            return false;
        }

        if(!_dataObj)
        {
            return false;
        }

        _acousticsPhysicsPropManager = dynamic_cast<Acoustics::FITKAcousticsNCTManager*>(_dataObj);

        if(!_acousticsPhysicsPropManager)
        {
            return false;
        }

        bool b = true;
        b &= readNCTLayUp();
        b &= readUserDefined();
        b &= readMultipleNoise();
        return b;
    }

    bool VAOneXMLAdaptorNCT::adaptW()
    {
        // 指针判断是否为空
        if(!_node)
        {
            return false;
        }

        if(!_dataObj)
        {
            return false;
        }

        _acousticsPhysicsPropManager = dynamic_cast<Acoustics::FITKAcousticsNCTManager*>(_dataObj);

        if(!_acousticsPhysicsPropManager)
        {
            return false;
        }

        bool b = true;
        b &= writeNCTLayUp();
        b &= writeUserDefined();
        b &= writeMultipleNoise();
        return b;
    }

    bool VAOneXMLAdaptorNCT::readNCTLayUp()
    {
        rapidxml::xml_node<char>* node = _node->first_node("LayeredNoiseControlTreatment");

        if (!node)// 判断多重声学包节点是否存在
        {
            return true;
        }

        bool b = true;
        IO:: VAOneXMLAdaptorAcousticsNCTLayUp* adaptor{};

        // 循环处理每个多重生声学包的节点数据
        while (node)
        {
            Acoustics::FITKAcousticsNCTLayUp* data = new Acoustics::FITKAcousticsNCTLayUp;

            if(!adaptor)
            {
                adaptor = FITKIOADAPTORFACTORY->createT<IO::VAOneXMLAdaptorAcousticsNCTLayUp>("VAOneXML", data);
            }

            if(!adaptor)
            {
                //读写器创建异常
                consoleMessage(3,"create adaptor Error");
                return false;
            }

            adaptor->setNode(node);
            adaptor->setDataObject(data);
            b &= adaptor->adaptR();
            //根据名称获取对象
            auto m = _acousticsPhysicsPropManager->getDataByName(data->getDataObjectName());

            if(!m)
            {
                //返回为空 插入mData
                _acousticsPhysicsPropManager->appendDataObj(data);
            }
            else
            {
                //返回不为空， 覆盖数据
                m->copy(data);
                delete data;
            }

            node = node->next_sibling("LayeredNoiseControlTreatment");
        }

        delete adaptor;
        adaptor = nullptr;
        //读取成功
        return b;
    }

    bool VAOneXMLAdaptorNCT::writeNCTLayUp()
    {
        //根据类型获取多层数据集合
        QList<Acoustics::FITKAcousticsAbsNCT*> dataList = _acousticsPhysicsPropManager->getAcousticsNCTByType(Acoustics::FITKAcousticsAbsNCT::NCTType::TreatmentLayUp);
        bool b = true;
        //获取数据集合数量
        int dataCount = dataList.count();
        IO:: VAOneXMLAdaptorAcousticsNCTLayUp* adaptor{};

        for(int i = 0; i < dataCount; i++)
        {
            auto NCTLayUpData = dataList[i];

            if(!NCTLayUpData)
            {
                continue;
            }

            //创建多层节点
            rapidxml::xml_node<char>* node = createNodeElement(_node, "LayeredNoiseControlTreatment");
            //创建多层节点操作器

            if(!adaptor)
            {
                adaptor = FITKIOADAPTORFACTORY->createT<IO::VAOneXMLAdaptorAcousticsNCTLayUp>("VAOneXML", NCTLayUpData);
            }

            if(!adaptor)
            {
                //读写器创建异常
                consoleMessage(3,"create adaptor Error");
                return false;
            }

            adaptor->setNode(node);
            adaptor->setDataObject(NCTLayUpData);
            b &= adaptor->adaptW();

        }

        delete adaptor;
        adaptor = nullptr;
        return b;
    }

    bool VAOneXMLAdaptorNCT::readUserDefined()
    {
        rapidxml::xml_node<char>* node = _node->first_node("UserDefinedNoiseControlTreatment");

        if (!node)// 判断多重声学包节点是否存在
        {
            return true;
        }

        bool b = true;
        IO:: VAOneXMLAdaptorAcousticsNCTUserDefined* adaptor{};

        // 循环处理每个多重生声学包的节点数据
        while (node)
        {
            Acoustics::FITKAcousticsNCTUserDefined* data = new Acoustics::FITKAcousticsNCTUserDefined;

            if(!adaptor)
            {
                adaptor = FITKIOADAPTORFACTORY->createT<IO::VAOneXMLAdaptorAcousticsNCTUserDefined>("VAOneXML", data);
            }

            if(!adaptor)
            {
                //读写器创建异常
                consoleMessage(3,"create adaptor Error");
                return false;
            }

            adaptor->setNode(node);
            adaptor->setDataObject(data);
            b &= adaptor->adaptR();
            //根据名称获取对象
            auto m = _acousticsPhysicsPropManager->getDataByName(data->getDataObjectName());

            if(!m)
            {
                //返回为空 插入mData
                _acousticsPhysicsPropManager->appendDataObj(data);
            }
            else
            {
                //返回不为空， 覆盖数据
                m->copy(data);
                delete data;
            }

            node = node->next_sibling("UserDefinedNoiseControlTreatment");
        }

        delete adaptor;
        adaptor = nullptr;
        //读取成功
        return b;
    }

    bool VAOneXMLAdaptorNCT::writeUserDefined()
    {
        //根据类型获取多层数据集合
        QList<Acoustics::FITKAcousticsAbsNCT*> dataList = _acousticsPhysicsPropManager->getAcousticsNCTByType(Acoustics::FITKAcousticsAbsNCT::NCTType::UserDefTreatment);
        bool b = true;
        //获取数据集合数量
        int dataCount = dataList.count();
        //创建多层节点操作器
        IO:: VAOneXMLAdaptorAcousticsNCTUserDefined* adaptor{};

        for(int i = 0; i < dataCount; i++)
        {
            auto NCTLayUpData = dataList[i];

            if(!NCTLayUpData)
            {
                continue;
            }

            //创建多层节点
            rapidxml::xml_node<char>* node = createNodeElement(_node, "UserDefinedNoiseControlTreatment");

            if(!adaptor)
            {
                adaptor = FITKIOADAPTORFACTORY->createT<IO::VAOneXMLAdaptorAcousticsNCTUserDefined>("VAOneXML", NCTLayUpData);
            }

            if(!adaptor)
            {
                //读写器创建异常
                consoleMessage(3,"create adaptor Error");
                return false;
            }

            adaptor->setNode(node);
            adaptor->setDataObject(NCTLayUpData);
            b &= adaptor->adaptW();
        }

        delete adaptor;
        adaptor = nullptr;
        return b;
    }

    bool VAOneXMLAdaptorNCT::readMultipleNoise()
    {
        rapidxml::xml_node<char>* node = _node->first_node("MultipleNoiseControlTreatment");

        if (!node)// 判断多重声学包节点是否存在
        {
            return true;
        }

        bool b = true;
        IO:: VAOneXMLAdaptorAcousticsNCTMultipleNoiseControl* adaptor{};

        // 循环处理每个多重生声学包的节点数据
        while (node)
        {
            Acoustics::FITKAcousticsNCTMultipleNoise* data = new Acoustics::FITKAcousticsNCTMultipleNoise;

            if(!adaptor)
            {
                adaptor = FITKIOADAPTORFACTORY->createT<IO::VAOneXMLAdaptorAcousticsNCTMultipleNoiseControl>("VAOneXML", data);
            }

            if(!adaptor)
            {
                //读写器创建异常
                consoleMessage(3,"create adaptor Error");
                return false;
            }

            adaptor->setNode(node);
            adaptor->setDataObject(data);
            b &= adaptor->adaptR();
            //根据名称获取对象
            auto m = _acousticsPhysicsPropManager->getDataByName(data->getDataObjectName());

            if(!m)
            {
                //返回为空 插入mData
                _acousticsPhysicsPropManager->appendDataObj(data);
            }
            else
            {
                //返回不为空， 覆盖数据
                m->copy(data);
                delete data;
            }

            node = node->next_sibling("MultipleNoiseControlTreatment");
        }

        delete adaptor;
        adaptor = nullptr;
        //读取成功
        return b;
    }

    bool VAOneXMLAdaptorNCT::writeMultipleNoise()
    {
        //根据类型获取多层数据集合
        QList<Acoustics::FITKAcousticsAbsNCT*> dataList = _acousticsPhysicsPropManager->getAcousticsNCTByType(Acoustics::FITKAcousticsAbsNCT::NCTType::MulitipleNCT);
        bool b = true;
        //获取数据集合数量
        int dataCount = dataList.count();
        //创建多层节点操作器
        IO:: VAOneXMLAdaptorAcousticsNCTMultipleNoiseControl* adaptor{};


        for(int i = 0; i < dataCount; i++)
        {
            auto NCTLayUpData = dataList[i];

            if(!NCTLayUpData)
            {
                continue;
            }

            //创建多层节点
            rapidxml::xml_node<char>* node = createNodeElement(_node, "MultipleNoiseControlTreatment");
            if(!adaptor)
            {
                adaptor = FITKIOADAPTORFACTORY->createT<IO::VAOneXMLAdaptorAcousticsNCTMultipleNoiseControl>("VAOneXML", NCTLayUpData);
            }

            if(!adaptor)
            {
                //读写器创建异常
                consoleMessage(3,"create adaptor Error");
                return false;
            }

            adaptor->setNode(node);
            adaptor->setDataObject(NCTLayUpData);
            b &= adaptor->adaptW();

        }

        delete adaptor;
        adaptor = nullptr;
        return b;
    }
}



